//
//  NovelDataBase.m
//  UI 19_数据库
//
//  Created by dllo on 15/5/11.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "NovelDataBase.h"
#import "NovelModel.h"
#import "DB.h"
@implementation NovelDataBase




+ (void)DB:(NSString *)str
{
    sqlite3 *db = [DB openDB];
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"更新成功");
    } else {
        NSLog(@"更新失败");
    }
}



+ (void)insertWithModel:(NovelModel *)model
{
  
    NSString *str = [NSString stringWithFormat:@"insert into Minfo(songName, singerName,songId) values ('%@', '%@', '%@')",model.song_name, model.singer_name, model.songId];
    
    

    
    // 获得数据库指针
    sqlite3 *db = [DB openDB];
    // 执行sql语句
    int result = sqlite3_exec(db, [str UTF8String], nil, nil, nil);
    // 判断是否执行成功
    if (result == SQLITE_OK) {
        
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [al show];
        [al release];
        
        
        
        
        
    }else {
       
        UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [al show];
        [al release];
        
    }
    
}

+ (void)deleteWithName:(NSString *)songName
{
    NSString *str = [NSString stringWithFormat:@"delete from Minfo where songName='%@'", songName];
    [NovelDataBase DB:str];
}

+ (void)updateWithName:(NSString *)bookName author:(NSString *)author bookid:(NSInteger)bookId
{
    NSString *str = [NSString stringWithFormat:@"update NovelList set book_name='%@',book_author='%@' where book_id='%ld'", bookName,author, bookId];
    [NovelDataBase DB:str];
}

// 查询所有
+ (NSArray *)selectAll
{
    NSString *str = @"select * from Minfo";
    sqlite3 *db = [DB openDB];
    // 定义执行结果接收者
    sqlite3_stmt *stmt = nil;
    
    // -1代表sql语句的长度没有限制
    // &stmt代表查询的结果所在的位置
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
         
            const unsigned char *songName = sqlite3_column_text(stmt, 0);
            const unsigned char *singerName = sqlite3_column_text(stmt, 1);
            const unsigned char *songId = sqlite3_column_text(stmt, 2);
        
            NSString *name = [NSString stringWithUTF8String:(const char*)songName];
            NSString *singer = [NSString stringWithUTF8String:(const char *)singerName];
            NSString *songid = [NSString stringWithUTF8String:(const char *)songId];
            
            NovelModel *model = [NovelModel modelWithSong_name:name singer_name:singer songId:songid ];
            [array addObject:model];
        }
    }
    return array;
}

// 查询单条
+ (NovelModel *)selectWithName:(NSString *)name
{
    NSString *str = [NSString stringWithFormat:@"select mima from Minfo where name='%@'", name];
    sqlite3 *db = [DB openDB];
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(db, [str UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        if (sqlite3_step(stmt) == SQLITE_ROW) {
        
            const unsigned char *name = sqlite3_column_text(stmt, 0);
            const unsigned char *mima = sqlite3_column_text(stmt, 1);
       
            NSString *aname = [NSString stringWithUTF8String:(const char*)name];
            NSString *amima = [NSString stringWithUTF8String:(const char *)mima];
            
            NovelModel *model = [NovelModel modelWithName:aname mima:amima];
            return model;
        }
    }
    return nil;
}

@end
